package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.reader.entity.Book;
import com.imooc.reader.mapper.BookMapper;
import com.imooc.reader.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("bookService")
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public IPage<Book> paging(Long categoryId, String order, Integer page, Integer rows) {
        QueryWrapper<Book> QueryWrapper = new QueryWrapper<Book>();
        if (categoryId != null && categoryId != -1) {
            QueryWrapper.eq("category_Id", categoryId);
        }
        if (order != null) {
            if (order.equals("quantity")) {
                QueryWrapper.orderByDesc("evaluation_quantity");
            } else if (order.equals("score")) {
                QueryWrapper.orderByDesc("evaluation_score");
            }
        }
        Page<Book> p = new Page<>(page, rows);

        Page<Book> list = bookMapper.selectPage(p, QueryWrapper);

        return list;
    }

    @Override
    public Book selectById(Long bookId) {
        Book book = bookMapper.selectById(bookId);
        return book;
    }

    @Override
    public Book creatBook(Book book) {

        bookMapper.insert(book);
        return book;
    }

    @Override
    public IPage<Book> pagings(Long categoryId, String order, Integer page, Integer rows) {
        return null;
    }
}
